namespace ImapX
{
    using System;

    public class ImapClient
    {
        public Imap _client;
        private FolderCollection _folders;

        public ImapClient(string host, int port, bool useSsl)
        {
            this._client = new Imap(host, port, useSsl);
        }

        public bool Connect()
        {
            return this._client.Connect();
        }

        public void CreateFolder(string name)
        {
            _client.CreateFolder(name);
        }

        public void Disconnect()
        {
            _client.Disconnect();
        }

        public void LogIn(string login, string password)
        {
            _client.LogIn(login, password);
        }

        public void LogOut()
        {
            _client.LogOut();
        }

        public MessageCollection EndUtf8Searsh(byte[] b)
        {
            MessageCollection messages = this._client.SearchUtf8Data(b);
            if (messages != null)
            {
                foreach (Message message in messages)
                {
                    message._client = this._client;
                }
            }
            return messages;
        }

        public FolderCollection GetFolders()
        {
            FolderCollection folders = this._client.GetFolders("");
            foreach (Folder folder in folders)
            {
                folder._client = this._client;
            }
            return folders;
        }

        public Folder GetFolder(string folderPath)
        {
            string[] folders = folderPath.Split(_client._delimiter);
            Folder folder = Folders[folders[0]];

            int i = 1;
            while (folder != null && i <= folders.Length - 1)
            {
                folder = folder.SubFolders[folders[i]];
                i++;
            }
            return folder;
        }

        public FolderCollection Folders
        {
            get
            {
                if (this._folders == null)
                {
                    this._folders = this.GetFolders();
                }
                return this._folders;
            }
            set
            {
                this._folders = value;
            }
        }

        public bool IsConnected
        {
            get
            {
                return this._client._isConnected;
            }
        }

        public bool IsDebug
        {
            get
            {
                return this._client.IsDebug;
            }
            set
            {
                this._client.IsDebug = value;
            }
        }

        public bool IsLogined
        {
            get
            {
                return this._client._isLogged;
            }
        }

        public string SelectedFolder
        {
            get
            {
                return this._client._selectedFolder;
            }
        }
    }
}

